package utl.mofen.resultSet;

import java.sql.Date;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

public class ConvertUtil {

	public static String rs2JsonString(ResultSet rs, boolean deleteFirstCharOnFieldName) throws SQLException {
		ResultSetMetaData rsmd = rs.getMetaData();
		String columnName = "";
		Object columnValue = null;
		int columntype = 0;

		String strValue = "";

		StringBuilder sb = new StringBuilder();
		sb.append("{\n");
		for (int j = 1; j <= rsmd.getColumnCount(); j++) {
			columnName = rsmd.getColumnName(j);
			columntype = rsmd.getColumnType(j);
			columnValue = rs.getObject(columnName);			
			if (columnValue != null) {
				if (deleteFirstCharOnFieldName)
				{
					sb.append("   \"" + columnName.substring(1).toLowerCase() + "\":");
				}else
				{
					sb.append("   \"" + columnName.toLowerCase() + "\":");
				}
				switch (columntype) {
				case Types.TIMESTAMP:
					Timestamp dt = rs.getTimestamp(j);
					SimpleDateFormat datef = new SimpleDateFormat(
							"yyyy-MM-dd HH:mm:ss");
					strValue = datef.format(dt);
					sb.append("\"" + strValue + "\"");
					break;
				case Types.DATE:
					Date dtValue = rs.getDate(columnName);
					DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
					strValue = format1.format(dtValue);
					sb.append("\"" + strValue + "\"");
					break;
				case Types.CHAR:
				case Types.VARCHAR:
					strValue = rs.getString(columnName);
					strValue = escape4JSon(strValue);
					sb.append("\"" + strValue + "\"");
					break;
					
				case Types.SMALLINT:
				case Types.TINYINT:
				case Types.DOUBLE:
				case Types.BIGINT:
				case Types.NUMERIC:
				case Types.DECIMAL:
				case Types.REAL:	
				case Types.INTEGER:
					strValue = rs.getString(j);
					sb.append(strValue);
					break;
				case Types.BIT:
				case Types.BOOLEAN:
					if (rs.getBoolean(j)) {
						strValue = "true";
					} else {
						strValue = "false";
					}
					sb.append(strValue);
					break;
				default:
					strValue = rs.getString(j);
					strValue = escape4JSon(strValue);
					sb.append("\"" + strValue + "\"");
					System.out.print("处理Default:" + columnName + " --->");
					System.out.println("type:" + columntype);
					break;
				}
				sb.append(",\n");
			} // end if
		}// end for
		if (sb.length() > 3)
			sb.deleteCharAt(sb.length()-2);
		sb.append("}");

		return sb.toString();
	}

	private static String escape4JSon(String s) {
		if (s == null)
			return "";
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < s.length(); i++) {
			char ch = s.charAt(i);
			switch (ch) {
			case '"':
				sb.append("\\\"");
				break;
			case '\\':
				sb.append("\\\\");
				break;
			case '\b':
				sb.append("\\b");
				break;
			case '\f':
				sb.append("\\f");
				break;
			case '\n':
				sb.append("\\n");
				break;
			case '\r':
				sb.append("\\r");
				break;
			case '\t':
				sb.append("\\t");
				break;
			case '/':
				sb.append("\\/");
				break;
			default:
				if ((ch >= 0) && (ch <= '\037')) {
					String ss = Integer.toHexString(ch);
					sb.append("\\u");
					for (int k = 0; k < 4 - ss.length(); k++) {
						sb.append('0');
					}
					sb.append(ss.toUpperCase());
				} else {
					sb.append(ch);
				}
			}
			
		}
		return sb.toString();
	}
}
